package com.sduept.nwld.test;

import lombok.extern.slf4j.Slf4j;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


@Slf4j
public class GenBreakerRelation extends DBConnection {
	

	private Map<String, Set<String>> equipments_nodes = new HashMap<>();//设备连接点的Map<设备Id,链接点集合>
	private Map<String, Set<String>> nodes_equipments = new HashMap<>();//设备连接点的Map<链接点,设备集合>
	private Set<String> breakerIds=new HashSet<>();
	private Set<String> compensatorIds=new HashSet<>();
	private Set<String> distIds=new HashSet<>();
	
	public GenBreakerRelation(){
		initData();
	}
	
	public void initData(){
		String getTerminals = "select conductingEquipment,connectivityNode from t_cim_terminal";
		java.sql.Statement st = null;
		getConnection();
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(getTerminals);
			while (rs.next()) {
				String conductingEquipment = rs.getString(1);
				String connectivityNode = rs.getString(2);

				Set<String> ens = equipments_nodes.get(conductingEquipment);
				if (ens == null) {
					ens = new HashSet<>();
					equipments_nodes.put(conductingEquipment, ens);
				}
				ens.add(connectivityNode);
				Set<String> nes = nodes_equipments.get(connectivityNode);
				if (nes == null) {
					nes = new HashSet<>();
					nodes_equipments.put(connectivityNode, nes);
				}
				nes.add(conductingEquipment);
			}
			rs.close();
		} catch(Exception e){
			e.printStackTrace();
		}finally {
			if (st != null){
				try {
					st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		initBreakerIds();
		initDistIds();
		initCompensatorIds();
//		String breakerSql="select rdfId from t_cim_breaker";
//		breakerIds.addAll((List<String>)dao.nativeQuery(breakerSql)) ;//断路器
//		String comSql="select rdfId from t_cim_compensator";
//		compensatorIds.addAll((List<String>)dao.nativeQuery(comSql));//电抗器电容器
//		String distSql="select rdfId from t_cim_disconnector";
//		distIds.addAll((List<String>)dao.nativeQuery(distSql));//刀闸
	}
	private void initBreakerIds(){
		java.sql.Statement st = null;
		try {
			getConnection();
			st = conn.createStatement();
			ResultSet rs = st.executeQuery("select rdfId from t_cim_breaker");
			while (rs.next()) {
				String id = rs.getString(1);
				breakerIds.add(id);
			}
			rs.close();
		} catch(Exception e){
			e.printStackTrace();
		}finally {
			if (st != null){
				try {
					st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	private void initCompensatorIds(){
		java.sql.Statement st = null;
		try {
			getConnection();
			st = conn.createStatement();
			ResultSet rs = st.executeQuery("select rdfId from t_cim_compensator");
			while (rs.next()) {
				String id = rs.getString(1);
				compensatorIds.add(id);
			}
			rs.close();
		} catch(Exception e){
			e.printStackTrace();
		}finally {
			if (st != null){
				try {
					st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	private void initDistIds(){
		java.sql.Statement st = null;
		try {
			getConnection();
			st = conn.createStatement();
			ResultSet rs = st.executeQuery("select rdfId from t_cim_disconnector");
			while (rs.next()) {
				String id = rs.getString(1);
				distIds.add(id);
			}
			rs.close();
		} catch(Exception e){
			e.printStackTrace();
		}finally {
			if (st != null){
				try {
					st.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if(conn!=null){
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}
	/**
	 * 根据拓扑关系获取断路器链接的刀闸
	 * @param breakerId
	 * @return
	 */
	public Set<String> getDisconnectorsByBreakerId(String breakerId){
		Set<String> result = new HashSet<String>();
		Set<String> cns = equipments_nodes.get(breakerId);// 获取所有连接点根据线路id
		if (cns != null) {
			for (String n : cns) {
				Set<String> tmps = nodes_equipments.get(n);// 根据所有连接点获取连接点连接的设备
				for (String e : tmps) { // 遍历找出设备是刀闸的连接点
					if (distIds.contains(e)) {
						result.add(e);
					}
				}
			}
		}
		return result;
	}
	public Set<String> getBreakerById(String id) throws Exception {
		Set<String> bs = new HashSet<>();
		Set<String> cns = equipments_nodes.get(id);// 获取所有连接点根据线路id
		if (cns != null) {
			Set<String> disconnectors = new HashSet<>();
			for (String n : cns) {
				Set<String> tmps = nodes_equipments.get(n);// 根据所有连接点获取连接点连接的设备
				disconnectors.addAll(tmps);
			}
			Set<String> disconnectors_cns2 = new HashSet<>();// 所有刀闸设备的连接点
			for (String e : disconnectors) { // 遍历找出设备是刀闸的连接点
				if (distIds.contains(e)) {
					Set<String> tmps = equipments_nodes.get(e);
					disconnectors_cns2.addAll(tmps);
				}
			}

			Set<String> disconnectors_es2 = new HashSet<>();// 根据所有刀闸连接点找出刀闸连接的设备
			for (String n : disconnectors_cns2) {
				Set<String> tmps = nodes_equipments.get(n);
				disconnectors_es2.addAll(tmps);
			}
			for (String e : disconnectors_es2) {// 遍历所有刀闸连接的设备找出断路器
				if (breakerIds.contains(e)) {
					bs.add(e);
				}
			}

			Set<String> cns3 = new HashSet<>();
			for (String e : disconnectors_es2) {
				if (distIds.contains(e)) {
					Set<String> tmps = equipments_nodes.get(e);
					cns3.addAll(tmps);
				}
			}
			Set<String> es3 = new HashSet<>();
			for (String n : cns3) {
				Set<String> tmps = nodes_equipments.get(n);
				es3.addAll(tmps);
			}

			for (String e : es3) {
				if (breakerIds.contains(e)) {
					bs.add(e);
				}
			}
			Set<String> cns4 = new HashSet<>();
			for (String e : es3) {
				if (distIds.contains(e)) {
					Set<String> tmps = equipments_nodes.get(e);
					cns4.addAll(tmps);
				}
			}
			/*
			 * for (String e : es3) { if (compensatorIds.contains(e)) {
			 * Set<String> tmps = equipments_nodes.get(e); for(String sid :
			 * tmps) { cns4.remove(sid); } } }
			 */

			Set<String> es4 = new HashSet<>();
			for (String n : cns4) {
				Set<String> tmps = nodes_equipments.get(n);
				es4.addAll(tmps);
			}

			for (String e : es4) {
				if (breakerIds.contains(e)) {
					bs.add(e);
				}
			}
			Iterator<String> it = bs.iterator();
			outer: while (it.hasNext()) {
				Set<String> tmps = equipments_nodes.get(it.next());// 拿到断路器的连接点
				for (String bcn : tmps) {
					Set<String> ces = nodes_equipments.get(bcn);
					for (String ce : ces) {
						if (compensatorIds.contains(ce)) {
							it.remove();
							continue outer;
						}
					}
				}
			}
		}

		return bs;
	}


	public static void main(String[] args) {
		try {
			Set<String> breakerByIds = new GenBreakerRelation().getBreakerById("5348024740216833");
			for (String id : breakerByIds) {
				log.info(id);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
